package yxy.game.pm2.command.achievement;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TaskMessage;
import yxy.game.pm2.module.achievement.AchievementModule;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.Map;

/** 任务成就 成就进度获取 */
@MessageType(proto = Command.AchievementProgressAcquire, classtype = TaskMessage.AchievementProgressAcquireRequest.class)
public class CMD_AchievementProgressAcquire extends Command<TaskMessage.AchievementProgressAcquireRequest> {

	@Override
	public void execute(MessageSource messageSource, TaskMessage.AchievementProgressAcquireRequest request) {
		String playerCode = messageSource.getTag();

		TaskMessage.AchievementProgressAcquireResponse.Builder response = TaskMessage.AchievementProgressAcquireResponse.newBuilder();

		Map<String, AchievementModule> all = AchievementModule.all(playerCode);
		Map<String, Task> allTask = Task.all(playerCode);
		for (AchievementModule module : all.values()) {
			if (module.getTaskCodes().size() <= 0) {
				continue;
			}
			String taskCode = module.getTaskCodes().get(0);
			Task task = allTask.get(taskCode);
			if (task == null) {
				continue;
			}
			response.addTasks(TaskHandle.packTask(task));
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, TaskMessage.AchievementProgressAcquireResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}